// 服务器端渲染
const express = require('express')
const { createSSRApp } = require('vue')
const { renderToString } = require('@vue/server-renderer')
const manifest = require('../dist/ssr-manifest.json')
const path = require('path')

const server = express()

const appPath = path.join(__dirname, '../dist', manifest['app.js'])

const App = require(appPath)

server.use('./img', express.static(path.join(__dirname, '../dist', 'img')))
server.use('./js', express.static(path.join(__dirname, '../dist', 'js')))
server.use('./css', express.static(path.join(__dirname, '../dist', 'css')))
server.use('./favicon', express.static(path.join(__dirname, '../dist', 'favicon.ico')))

server.get('*', async (req, res) => {
  const app = createSSRApp(App)
  const appContent = await renderToString(app)
  const html = `
  <html>
    <head></head>
    <title>Hello</title>
    <link rel="stylesheet" href="${manifest['app.css']}"></link>
  </html>
  <body>
  ${appContent}
  </body>
  `
  res.end(html)
})

server.listen(8080,()=>{
  
})